//
// Created by chizuru on 2025/9/17.
//

#pragma once
#include "DeviceDecorator.h"
#include <iostream>
#include <any>
#include <iomanip>

class LoggingDecorator : public DeviceDecorator {
public:
    explicit LoggingDecorator(std::shared_ptr<IDeviceIO> inner) : DeviceDecorator(std::move(inner)) {}
    int write(const char* data, size_t size) override {
        std::cerr << "[DEV WRITE]  bytes = " << size << " data = " ;
        for (size_t i = 0; i < size; i++)
            std::cerr << std::hex << std::setw(2) << std::setfill('0')
                << static_cast<short>(data[i]) << " ";
        std::cerr << "\n";
        int r = DeviceDecorator::write(data, size);
        std::cerr << "[DEV WRITE] ret = " << r << "\n";
        return r;
    }
    int read(char* buf, size_t maxSize) override {
        int r = DeviceDecorator::read( buf, maxSize);
        std::cerr << "[DEV READ] bytes = " << r << " data = ";
        for (int i = 0; i < r; i++)
            std::cerr << std::hex << std::setw(2) << std::setfill('0')
                << static_cast<short>(buf[i]) << " ";
        std::cerr << "\n";
        return r;
    }

    int flushReadAndWrite() override {
        return DeviceDecorator::flushReadAndWrite();
    }
};